<template>
	<view>
		<view>
			<!-- #ifdef H5 -->
			<uni-drawer ref="showLeft" mode="left" :mask-click="true">
			<!-- #endif -->
			<!-- #ifdef MP-WEIXIN -->
			<uni-drawer ref="showLeft" mode="left" :mask-click="true" width="260">
			<!-- #endif -->
				<scroll-view style="height: 100%;" scroll-y="true">
					<view style="display: flex;flex-direction: column;">
						<view>
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:35%; text-align: left;">工单编号</view>
								<view style="font-size:medium; color:#000000; width:60%; text-align: right;">
									{{orderData.id}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view>
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:35%; text-align: left;">维修地址</view>
								<view style="font-size:medium; color:#000000; width:60%; text-align: right;">
									{{orderData.address}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view>
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:35%; text-align: left;">维修原因</view>
								<view style="font-size:medium; color:#000000; width:60%; text-align: right;">
									{{orderData.reason}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view>
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:35%; text-align: left;">提交人</view>
								<view style="font-size:medium; color:#000000; width:60%; text-align: right;">
									{{orderData.uname}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view>
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:45%; text-align: left;">提交人联系方式
								</view>
								<view style="font-size:medium; color:#000000; width:50%; text-align: right;">
									{{orderData.uphone}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view>
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:35%; text-align: left;">提交时间</view>
								<view style="font-size:medium; color:#000000; width:60%; text-align: right;">
									{{orderData.starttime}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view v-if="orderData.consumable!=null && orderData.consumable!=''">
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:45%; text-align: left;">维修耗材
								</view>
								<view style="font-size:medium; color:#000000; width:50%; text-align: right;">
									{{orderData.consumable}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view>
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:35%; text-align: left;">当前状态</view>
								<view style="font-size:medium; color:#000000; width:60%; text-align: right;">
									{{orderData.status}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view v-if="orderData.workerreason!=null && orderData.workerreason!=''">
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:35%; text-align: left;">维修人员退单原因
								</view>
								<view style="font-size:medium; color:#000000; width:60%; text-align: right;">
									{{orderData.workerreason}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view v-if="orderData.adminreason!=null && orderData.adminreason!=''">
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:35%; text-align: left;">管理员拒绝退单原因
								</view>
								<view style="font-size:medium; color:#000000; width:60%; text-align: right;">
									{{orderData.adminreason}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view>
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<view style="font-size:medium; color:#888888; width:35%; text-align: left;">更新时间</view>
								<view style="font-size:medium; color:#000000; width:60%; text-align: right;">
									{{orderData.updatetime}}
									<view style="width: 5%;"></view>
								</view>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>

						<view>
							<view style="height: 20rpx;"></view>
							<view style="display: flex;flex-direction: row;">
								<button size="mini"
									:disabled="orderData.imgsfileids1==null||orderData.imgsfileids1=='[]'"
									type="primary" plain="true" @click="lookImg(orderData.imgsfileids1)">查看申报图片</button>
								<button size="mini" type="primary" plain="true"
									@click="agreeApply(orderData.id)">确认接单</button>
								<button size="mini" type="warn" plain="true"
									@click="refuseApply(orderData.id)">申请退单</button>
							</view>
							<view style="height: 20rpx;"></view>
							<view style="background: #E0E3DA;width: 100%;height: 5rpx;"></view>
						</view>
					</view>
				</scroll-view>
			</uni-drawer>
		</view>
		<view>
			<modal v-if="showPop" title="申请撤单" confirm-text="确定" cancel-text="取消" @cancel="cancelPop"
				@confirm="confirmPop">
				<input type='text' placeholder="请输入理由" v-model="workerreason" />
			</modal>
		</view>
		<view>
			<uni-table border stripe emptyText="暂无更多数据">
				<!-- 表头行 -->
				<uni-tr>
					<uni-th width="120">提交时间</uni-th>
					<uni-th width="120">维修地址</uni-th>
					<uni-th width="150">维修原因</uni-th>
				</uni-tr>
				<!-- 表格数据行 -->
				<uni-tr v-for="item in tableData" @click.native="showDrawer(item)">
					<uni-td>{{item.starttime}}</uni-td>
					<uni-td>{{item.address}}</uni-td>
					<uni-td>{{item.reason}}</uni-td>
				</uni-tr>
			</uni-table>
		</view>
	</view>
</template>

<script>
	import utils from "../../../../../utils.js"
	export default {
		data() {
			return {
				tableData: null,
				id: null,
				showPop: false, //弹窗
				workerreason: null,
				orderData: {
					address: null,
					adminreason: null,
					consumable: null,
					id: null,
					imgsfileids1: null,
					imgsfileids2: null,
					left: null,
					mark: null,
					reason: null,
					right: null,
					selectEndTime: null,
					selectStartTime: null,
					starttime: null,
					status: null,
					type: null,
					uid: null,
					uname: null,
					updatetime: null,
					uphone: null,
					wid: null,
					wname: null,
					workernote: null,
					workerreason: null,
					wphone: null
				}
			}
		},
		methods: {
			showDrawer(item) {
				console.log("点击了")
				console.log(item)
				this.orderData = item
				this.$refs.showLeft.open();
			},
			agreeApply(id) {
				const that = this
				uni.showModal({
					title: "工单确认",
					content: "确认接受该工单吗",
					success: function(res) {
						if (res.confirm) {
							uni.showLoading({});
							var token = getApp().globalData.token
							uni.request({
								url: getApp().globalData.BASE_URL + '/worker/confirmOrder',
								data: {
									token: token,
									id: id
								},
								header: {},
								success(response) {
									console.log(response)
									uni.hideLoading()
									if (response.data.status === 444) {
										uni.showToast({
											title: '您的登录信息已过期，请重新登录',
											duration: 2000,
											icon: "none",
											success() {
												setTimeout(function() {
													uni.reLaunch({
														url: "/pages/login/login",
													})
												}, 2000)
											}
										});
									} else if (response.data.status === 445) {
										uni.showToast({
											title: '您没有此操作权限',
											duration: 2000,
											icon: "none"
										});
									} else if (response.data === 1) {
										uni.showToast({
											title: '接单成功',
											duration: 2000,
											success() {
												setTimeout(function() {
													that.$refs.showLeft.close();
													that.selectOrders()
												}, 2000)
											}
										});
									} else {
										uni.showToast({
											title: '接单失败',
											duration: 2000,
											icon: "none"
										});
									}
								}
							})
						}
					}
				})
			},
			refuseApply(id) {
				this.id = id
				this.showPop = true
			},
			//弹窗
			confirmPop() { //确认
				if (this.workerreason == null || this.workerreason == "") {
					uni.showToast({
						title: "请填写退单理由",
						icon: "none"
					})
				} else {
					const that = this
					uni.showLoading({});
					var token = getApp().globalData.token
					uni.request({
						url: getApp().globalData.BASE_URL + '/worker/cancelOrder',
						data: {
							token: token,
							id: that.id,
							workerReason: that.workerreason
						},
						header: {},
						success(response) {
							console.log(response)
							uni.hideLoading()
							if (response.data.status === 444) {
								uni.showToast({
									title: '您的登录信息已过期，请重新登录',
									duration: 2000,
									icon: "none",
									success() {
										setTimeout(function() {
											uni.reLaunch({
												url: "/pages/login/login",
											})
										}, 2000)
									}
								});
							} else if (response.data.status === 445) {
								uni.showToast({
									title: '您没有此操作权限',
									duration: 2000,
									icon: "none"
								});
							} else if (response.data === 1) {
								that.showPop = false
								that.id = null
								that.workerreason = null
								uni.showToast({
									title: '提交成功',
									duration: 2000,
									success() {
										setTimeout(function() {
											that.$refs.showLeft.close();
											that.selectOrders()
										}, 2000)
									}
								});
							} else {
								uni.showToast({
									title: '提交失败',
									duration: 2000,
									icon: "none"
								});
							}
						}
					})
				}
			},
			cancelPop() { //取消
				this.workerreason = null
				this.showPop = false
			},
			lookImg(imgs) {
				console.log(imgs)
				imgs = JSON.parse(imgs)
				for (var i = 0; i < imgs.length; i++) {
					imgs[i] = (getApp().globalData.BASE_URL + "/upload/" + imgs[i]).replace(/[\r\n]/g, "")
				}
				console.log(imgs)
				uni.previewImage({
					urls: imgs,
				});
			},
			selectOrders() {
				const that = this
				uni.showLoading({});
				var token = getApp().globalData.token
				uni.request({
					url: getApp().globalData.BASE_URL + '/worker/selectOrders',
					data: {
						token: token,
						status: '管理员已派单',
					},
					header: {},
					success(response) {
						console.log(response)
						uni.hideLoading()
						if (response.data.status === 444) {
							uni.showToast({
								title: '您的登录信息已过期，请重新登录',
								duration: 2000,
								icon: "none",
								success() {
									setTimeout(function() {
										uni.reLaunch({
											url: "/pages/login/login",
										})
									}, 2000)
								}
							});
						} else if (response.data.status === 445) {
							uni.showToast({
								title: '您没有此操作权限',
								duration: 2000,
								icon: "none"
							});
						} else {
							that.tableData = response.data.orders
							let newConfirmOrdersNum = response.data.orders.length
							utils.$emit("updateConfirmOrdersNum", newConfirmOrdersNum)
						}
					}
				})

			},
		},
		created() {

		},
		onShow() {
			this.selectOrders()
		}
	}
</script>

<style>

</style>
